package scr 
{
	import cell.KeyPad;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import ru.gosoplata.model.TmpModel;
	import ru.gosoplata.view.FooterView;
	import uicontrols.ButtonBase;
	/**
	 * ...
	 * @author a.orleansky
	 */
	public class ScreenEnterPhone extends ScreenBase implements IScreen
	{
		public var keypad:KeyPad;
		private var inputtxt0:TextField;
		private var inputtxt1:TextField;
		private var inputtxt2:TextField;
		private var phone_nums_limit:Number = 10;

		public var btn_back:ButtonBase;
		public var btn_menu:ButtonBase;
		public var btn_next:ButtonBase;
		private var pic_count:Number=0;
		
		private var pics_info:Array = new Array(new Array("plane_tel", TmpModel.IMG_FOLDER+"/vvdod_nomera/images/plane_tel.png"),
												new Array("inbox_tel", TmpModel.IMG_FOLDER+"/vvdod_nomera/images/inbox_tel.png"),
												new Array("inbox_tel_big", TmpModel.IMG_FOLDER+"/vvdod_nomera/images/inbox_tel_big.png"),
												new Array("inbox_tel_chek", TmpModel.IMG_FOLDER+"/vvdod_nomera/images/inbox_tel_chek.png"),
												new Array("inbox_tel_big_chek", TmpModel.IMG_FOLDER+"/vvdod_nomera/images/inbox_tel_big_chek.png"));
		
		private var	up_txt_str:String = "Введите номер телефона";
		private var down_txt_str:String = "<FONT COLOR='#B40E10'>Номер телефона вводится без </FONT>«8» <FONT COLOR='#B40E10'>в формате</FONT> (ХХХ) ХХХ-ХХ-ХХ";
		private var up_txt:TextField;
		private var down_txt:TextField;
												
		private var curr_pic:Array;
		
		private var _footer:FooterView = new FooterView();
		
		
		public function ScreenEnterPhone() 
		{
			super("enter_phone");
		}
		
		override protected function init(e:Event):void 
		{
			super.init(e);
			
			addChild(_footer);
			
			keypad = new KeyPad();
			keypad.x = 600;
			keypad.y = 200;
			keypad.num_limit = phone_nums_limit;
			addChild(keypad);
			addInputField();
			createBtns();
			keypad.addEventListener(KeyPad.KEY_PAD_RELEASED, update);
		}
		
		private function update(e:Event):void 
		{
			if((e.target as KeyPad).entered_value.length <= 10){
				//TmpModel.userInfo.writePhone((e.target as KeyPad).entered_value);
				TmpModel.payment.validateAccount((e.target as KeyPad).entered_value);
			}
			reDraw2();
		}
				
		public function addInputField():void 
		{
			var txt_x:Number = 150;
			var txt_y:Number = 360;
			var txt_type:TextFormat = TmpModel.getTextFormat(52, 0x676767,true);
			txt_type.letterSpacing = 7;
			var txt_type3:TextFormat = TmpModel.getTextFormat(38, 0x676767,true);
			txt_type3.letterSpacing = 1;
			
			inputtxt0 = new TextField();
			inputtxt0.embedFonts = true;
			inputtxt0.y = txt_y;
			inputtxt0.x = txt_x+10;
			inputtxt0.width = 54;
			inputtxt0.height = 85;
			inputtxt0.border = false;
			inputtxt0.selectable = false;
			inputtxt0.defaultTextFormat = TmpModel.getTextFormat(52, 0xFFFFFF,true);
			inputtxt0.antiAliasType = AntiAliasType.ADVANCED;
			inputtxt0.thickness = 100;
			
			inputtxt1 = new TextField();
			inputtxt1.embedFonts=true;
			inputtxt1.y = txt_y;
			inputtxt1.x = txt_x + inputtxt0.width + 15;
			inputtxt1.width = 130;
			inputtxt1.height = 85;
			inputtxt1.border = false;
			inputtxt1.selectable = false;
			inputtxt1.defaultTextFormat = txt_type;
			inputtxt1.antiAliasType = AntiAliasType.ADVANCED;
			
			inputtxt2 = new TextField();
			inputtxt2.embedFonts = true;
			inputtxt2.y = txt_y;
			inputtxt2.x = inputtxt1.x + inputtxt1.width +15;
			inputtxt2.width = 350;
			inputtxt2.height = 85;
			inputtxt2.border = false;
			inputtxt2.selectable = false;
			inputtxt2.defaultTextFormat = txt_type;
			inputtxt2.antiAliasType = AntiAliasType.ADVANCED;
			
			up_txt = new TextField();
			up_txt.embedFonts = true;
			up_txt.y = txt_y - 60;
			up_txt.x = txt_x+15;
			up_txt.width = 550;
			up_txt.defaultTextFormat = txt_type3;
			up_txt.antiAliasType = AntiAliasType.ADVANCED;
			up_txt.thickness = -50;
			up_txt.text = up_txt_str;
			addChild(up_txt);
			
			down_txt = new TextField();
			down_txt.embedFonts = true;
			down_txt.defaultTextFormat = TmpModel.getTextFormat(16, 0x676767,true);
			down_txt.width = 550;
			down_txt.htmlText = down_txt_str;
			down_txt.x = up_txt.x;
			down_txt.y = txt_y + 85;
			down_txt.thickness = -200;
			down_txt.antiAliasType = AntiAliasType.ADVANCED;
			
			addChild(down_txt);
			
			pics_info = pics_info.reverse();
			loadPictures();
		}
		
		override public function reDraw():void
		{ 
			_footer.reDraw();
						
			keypad.rewrite_value = TmpModel.payment.accountId;//TODO: ScreenEnterPhone reDraw - и снова вью все само решает keypad.rewrite_value
			reDraw2();
		}
		
		public function reDraw2():void 
		{ 
			inputtxt0.text = "8";
			
			var ph:String = keypad.entered_value;
			inputtxt1.text = ph.substr(0, 3);
			ph = ph.substr(3);
			if (ph.length <= 3)	inputtxt2.text = ph;
			else if (ph.length <= 5) inputtxt2.text = ph.substr(0, 3) + "-" + ph.substr(3);
			else inputtxt2.text = ph.substr(0, 3) + "-" + ph.substr(3, 2) + "-" + ph.substr(5);
			
			getChildByName("next").visible = (TmpModel.payment.accountId != "");//TODO: event
		}
		
		private function loadPictures():void 
		{
			if(pics_info.length > 0){
				curr_pic = (pics_info.pop() as Array);
				
				var url:String = "";
				url = curr_pic[1];
				var req:URLRequest = new URLRequest(url);
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void { } );
				loader.load(req);
			}
		}
		
		public function placeImage(image:Bitmap):void {
			switch(curr_pic[0]) {
				case "plane_tel":
					image.x = 150;
					image.y = 350;
					addChild(image);
					addChild(inputtxt0);
					break;
				case "inbox_tel":
					image.x = 205;
					image.y = 353;
					addChild(image);
					addChild(inputtxt1);
					break;
				case "inbox_tel_big":
					image.x = 355;
					image.y = 353;
					addChild(image);
					addChild(inputtxt2);
					break;
				case "inbox_tel_check":
					image.x = 220;
					image.y = 350;
					image.visible = false;
					addChild(image);
					break;
				case "inbox_tel_big_check":
					image.x = 370;
					image.y = 350;
					image.visible = false;
					addChild(image);
					break;
				default:
					image.x = 0;
					image.y = stage.stageHeight - image.height;
				break;
			}
			loadPictures();
		}
		
		private function onLoadComplete(e:Event):void 
		{
			var ldr:Loader = Loader(e.target.loader);
			var image:Bitmap = Bitmap(ldr.content);
			placeImage(image);
		}
		
		private function createBtns():void 
		{ 			
			btn_next = new ButtonBase("next", 0x00FF00);
			btn_next.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_next.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			btn_next.visible = false;
			addChild(btn_next);
			pic_count++;
			//loadBanner(TmpModel.IMG_FOLDER+"/sotovie_operatori/images/reklama_title.png");
		}

		private function allPicLoaded(e:Event):void 
		{
			pic_count--;
			if (pic_count == 0) 
			{
				btn_next.x = stage.stageWidth - (btn_next.width + 20)
				btn_next.y = stage.stageHeight - (btn_next.height + 20);
				
				keypad.x = btn_next.x - 90;
				keypad.y = 300;
			}
		}
		
		private function pressBtn(e:Event):void {
			switch(e.target.name) 
			{
				case "next":
					TmpModel.tmpGlobalController.onClick("confirm_phone");
					break;
			}
		}
	}
}